/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sistemas.sisgear.JSQLParser;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.parser.CCJSqlParserManager;
import net.sf.jsqlparser.statement.Statement;
import net.sf.jsqlparser.statement.select.OrderByElement;
import net.sf.jsqlparser.statement.select.Select;
import net.sf.jsqlparser.statement.select.SelectItem;
/**
 *
 * @author Jean
 */
public class Validación {
    
    List<String> tables;
    ArrayList<SelectItem> listaSelect = new ArrayList<>();
    ArrayList<String> listaFrom = new ArrayList<>();
    ArrayList<OrderByElement> listaOrderBy = new ArrayList<>();

    public Validación() {
    }
    
    public boolean validarConsulta(String sql) {
        boolean valido = true;
        try {
            CCJSqlParserManager pm = new CCJSqlParserManager();
            Statement statement = pm.parse(new StringReader(sql));
            Select selectStatement = (Select) statement;

        } catch (Exception ex) {
            System.out.println(ex);
            valido = false;

        }
        return valido;
    }
    
    public boolean validarOrderBy(String cadena, String sub) {
        int resultado = cadena.indexOf(sub);
        boolean contiene = false;
        if (resultado != -1) {
            contiene = true;
        }

        return contiene;
    }
    
    public void cargarListas(String sql) throws JSQLParserException {

        TablesNamesFinder tablesNamesFinder = new TablesNamesFinder();

        CCJSqlParserManager pm = new CCJSqlParserManager();
        Statement statement = pm.parse(new StringReader(sql));

        Select selectStatement = (Select) statement;

        listaFrom = (ArrayList<String>) tablesNamesFinder.getTableList(selectStatement);
        listaOrderBy = (ArrayList<OrderByElement>) tablesNamesFinder.getOrderByElements(selectStatement);
        listaSelect = (ArrayList<SelectItem>) tablesNamesFinder.getSelectElements(selectStatement);
    }
    
    public void Mostrar(ArrayList lista) {
        for (int i = 0; i < lista.size(); i++) {
            System.out.print(" " + lista.get(i).toString());
        }
        System.out.println("");
    }
}
